﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Commons.Web.Helpers
{
    /// <summary>
    /// 提供一组重定向ＵＲＬ的操作
    /// </summary>
    public class ReturnUrl
    {

        #region Events
        /// <summary>
        /// 重定向事件
        /// </summary>
        public static event ReturnUrlEventHandler RedirectUrl;
        #endregion

        #region OnEvent Methods
        /// <summary>
        /// 触发重定向事件
        /// </summary>
        public static void OnRedirectUrl(string url)
        {
            if ((RedirectUrl != null))
            {
                RedirectUrl(null, new ReturnUrlEventArgs(url));
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// location.href页面重定向
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void ReturnUrl_RedirectUrl(object sender, ReturnUrlEventArgs e)
        {
            System.Web.HttpContext.Current.Response.Write("<script>window.location.href='" + e.Url + "';</script>");
        }

        /// <summary>
        /// window.open新窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void ReturnUrl_WindowOpen(object sender, ReturnUrlEventArgs e)
        {
            System.Web.HttpContext.Current.Response.Write("<script>window.open('','" + e.Url + "');</script>");
        }

        #endregion

    }
    #region Delegates
    /// <summary>
    /// 返回ＵＲＬ委托
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void ReturnUrlEventHandler(object sender, ReturnUrlEventArgs e);
    #endregion

    #region Event Argument
    /// <summary>
    /// ＵＲＬ数据源
    /// </summary>
    public class ReturnUrlEventArgs
    {
        public ReturnUrlEventArgs()
        { }
        public ReturnUrlEventArgs(string url)
        {
            this.Url = url;
        }
        public string Url { get; set; }
    }
    #endregion
}
